<?php

namespace app\common\model\distribute;

use app\common\model\LibUser;
use common\Common;

class DirectUserCommission extends base
{
    public function getCommissionByDirectUserId ()
    {
        $result = [];
        $this->write_file('-----------直推用户-----------');
        if (isset($this->orderInfo['direct_user_id']) && $this->orderInfo['direct_user_id'])
        {
            $directUserId = $this->orderInfo['direct_user_id'];
            $directUserMoney = $this->orderInfo['zt_commission_money'];
            $findDirectUserRes = $this->user->findUser(['id' => $directUserId]);
            $directUserInfo = $findDirectUserRes['data'];
            $this->write_file('直推用户：' . $directUserId);
            $this->write_file('直推用户身份：' . $directUserInfo['identity']);
            $this->write_file('直推用户佣金：' . $directUserMoney);

            if (strpos($directUserInfo['identity'], '0') !== false) { //是游客
                // 更新微信用户信息
                $data = $this->getData($directUserInfo, $directUserMoney);
                $this->user->updateUser(['id' => $directUserId], $data);
                $sql1 = $this->user->getLastSql();
                $this->write_file('更新会员佣金SQL：' . $this->user->getLastSql());
                // 添加会员佣金记录
                $sql2 = $this->extracted($directUserMoney, $directUserInfo['balance'], $directUserId);
                $result[] = [
                    'update_member_balance_sql' => $sql1,
                    'create_member_commission_record_sql' => $sql2
                ];
            } else {
                $findDirectMemberRes = $this->member->findMember(['user_id' => $directUserId]);
                if ($findDirectMemberRes['errorCode'] == 0)
                {
                    $directMemberInfo = $findDirectMemberRes['data'];
                    $data = $this->getData($directMemberInfo, $directUserMoney);
                    $this->member->updateMember(['user_id' => $directUserId], $data);
                    $sql1 = $this->member->getLastSql();
                    $this->write_file('更新会员佣金SQL：' . $sql1);
                    $this->user->updateUser(['id' => $directUserId], $data);
                    $sql2 = $this->user->getLastSql();
                    $this->write_file('更新会员佣金SQL：' . $sql2);
                    // 添加会员佣金记录
                    $sql3 = $this->extracted($directUserMoney, $directMemberInfo['balance'], $directUserId);
                    $this->write_file('添加会员佣金记录SQL：' . $sql3);
                    $result[] = [
                        'update_member_balance1_sql' => $sql1,
                        'update_member_balance2_sql' => $sql2,
                        'create_member_commission_record_sql' => $sql3
                    ];

                    // 给分享人发送模板消息
                    $findUserRes = $this->user->findUser(['id' => $directUserId]);
                    $openid = $findUserRes['data']['account'];
                    $template_id = "voWbDcqldG43OvfAD0n7X0tASX5c_92iBC5DLIjVx8g";
                    $data4 = [
                        'first'=>array('value'=>'缘份驿站提醒您，您已获得平台收益', 'color'=>'#173177'),
                        'keyword1'=>array('value'=>$directMemberInfo['username'], 'color'=>'#173177'),
                        'keyword2'=>array('value'=>$directUserMoney.'元', 'color'=>'#173177'),
                        'keyword3'=>array('value'=>'平台支付奖励', 'color'=>'#173177'),
                        'remark'=>array('value'=>'缘份驿站提醒您，收益金额请在我的-收益明细中查看。', 'color'=>'#173177'),
                    ];
                    \wechatUtil\TemplateMessage::sendTemplateMessage($data4,$openid,$template_id);
                } else {
                    $this->write_file('用户不存在' );
                }
            }
        }

        return $result;
    }

    private function getData($memberInfo, $commission)
    {
        $total_balance = (float)$memberInfo['total_balance'] + (float)$commission;//累计佣金
        $balance = (float)$memberInfo['balance'] + (float)$commission;//可提现佣金
        $commission = (float)$memberInfo['commission'] + (float)$commission;//冻结佣金
        if ($this->type == 2 || $this->type == 3) {//当订单类型是 【红娘认证】【栏目认证】，所得金额进入冻结佣金字段
            $data['commission'] = $commission;
        } else {
            $data = [
                'total_balance' => $total_balance,
                'balance' => $balance,
            ];
        }
        return $data;
    }

    private function extracted($money, $balance, $userId)
    {
        $sql = '';
        if (floatval($money) > 0) {
            $type = $this->orderInfo['type'];
            $typeStr = '';
            if ($type == 0) $typeStr = '付费看视频';
            if ($type == 1) $typeStr = '购买VIP套餐';
            if ($type == 2) $typeStr = '栏目认证';
            if ($type == 3) $typeStr = '红娘认证';
            $curPayUser = $this->curPayUserInfo;
            if ($curPayUser['errorCode'] == 0) {
                $info = $curPayUser['data']['username'] . $typeStr . '，可得佣金：' . $money;
            } else {
                $info = $typeStr . '，可得佣金：' . $money;
            }
            $data = [];
            $data['user_id'] = $userId;
            $data['describe'] = $info;
            $data['before_balance'] = $balance;
            $data['amount'] = $money;
            $data['add_time'] = Common::getTime();
            $data['order_id'] = $this->orderInfo['id'];
            $data['pay_user_id'] = $this->orderInfo['user_id'];
            $data['type'] = $this->type;
            $data['zj_state'] = in_array($this->type, [2, 3]) ? 1 : 0;//佣金状态 0可提现 1冻结中 2已失效
            $data['order_info'] = json_encode($this->orderInfo, JSON_UNESCAPED_UNICODE);
            $this->balanceRecords->addCommissionRecords($data);
            $sql = $this->balanceRecords->getLastSql();
        }
        return $sql;
    }
}